package com.dingwen.treasure.kettle.execute;

import cn.hutool.extra.spring.SpringUtil;
import com.dingwen.treasure.kettle.bo.CarteBo;
import com.dingwen.treasure.kettle.config.KettleConfig;
import com.dingwen.treasure.kettle.constant.KettleConstant;
import io.swagger.models.auth.In;
import lombok.SneakyThrows;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 　AbstractKettleExecute
 * 　@author dingwen
 * 　@date 2022/8/21
 */
@Slf4j
public abstract class AbstractKettleExecute {


    /**
     * 请求参数
     */
    protected static ConcurrentHashMap<String, Object> requestParams;


    /**
     * 请求地址
     */
    protected static String requestUrl;


    /**
     * 初始化公共参数
     */
    static {
        KettleConfig kettleConfig = SpringUtil.getBean(KettleConfig.class);
        CarteBo carte = kettleConfig.getCarte();
        requestParams = new ConcurrentHashMap<>(8);
        requestParams.put(KettleConstant.KETTLE_CARTE_USER, carte.getUser());
        requestParams.put(KettleConstant.KETTLE_CARTE_PASS, carte.getPass());
        requestParams.put(KettleConstant.KETTLE_CARTE_LEVEL, carte.getLevel());
        requestParams.put(KettleConstant.KETTLE_CARTE_REP, carte.getRep());
        requestParams.put(KettleConstant.KETTLE_CARTE_AUTHORIZATION, carte.getAuthorization());


        requestUrl = carte.getBaseUrl().concat(":").concat(carte.getPort().toString());
    }

    /**
     * 执行
     *
     * @param fullFileName 完整文件名称
     * @return boolean
     */
    @SneakyThrows
    public void execute(String fullFileName) {
        log.info("当前执行任务线程：{}", Thread.currentThread().getName());
        run(fullFileName, builderRequestParams(fullFileName));
    }


    /**
     * 构建器请求参数
     *
     * @param fullFileName 完整文件名称
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    protected abstract Map<String, Object> builderRequestParams(String fullFileName);

    /**
     * 运行
     *
     * @param fullFileName  完整文件名称
     * @param requestParams 请求参数
     */
    protected abstract void run(String fullFileName, Map<String, Object> requestParams);


    /**
     * 构建请求url
     *
     * @param url           url
     * @param requestParams 请求参数
     * @return {@link String}
     */
    public String buildRequestUrl(String url, Map<String, Object> requestParams) {
        StringBuilder builder = new StringBuilder(url);
        builder.append("?");
        int index = 0;
        for (Map.Entry<String, Object> entry : requestParams.entrySet()) {
            index++;
            if (ObjectUtils.nullSafeEquals(KettleConstant.KETTLE_CARTE_AUTHORIZATION, entry.getKey())) {
                continue;
            }
            builder.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue());
            if (index != requestParams.size()) {
                builder.append("&");
            }
        }
        return builder.toString();
    }
}
